package com.promise.jokerdream.work.signin.after

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 活跃礼包工作
class ActiveGiftWork(override val uin: String): BaseWork(uin) {

    override val name: String
        get() = "活跃礼包"

    override val priority: Int
        get() = 8

    override val isActWork: Boolean
        get() = true
    
    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }

    override suspend fun executeWork(): WorkResult {
        try {
            // 1. 查询活跃礼包状态
            val queryActiveGiftResult = gameApi.queryActiveGift()
            return queryActiveGiftResult.foldWithApiException(
                onSuccess = { activeGiftResponse ->
                    // 2. 查询活跃度信息
                    val queryLivenessResult = gameApi.queryLiveness()
                    return queryLivenessResult.foldWithApiException(
                        onSuccess = { livenessResponse ->
                            val currentLiveness = livenessResponse.getCurrentLiveness()
                            
                            // 3. 如果isGetLow=0且活跃度大于等于50，领取50活跃度礼包
                            if (activeGiftResponse.isGetLow == 0 && currentLiveness >= 50) {
                                val getLowResult = gameApi.getActiveGiftLow()
                                getLowResult.foldWithApiException(
                                    onSuccess = { response ->
                                        historyManager.addHistory(name, response.getDescription())
                                    },
                                    onFailure = { code, msg ->
                                        historyManager.addHistory(name, msg, code)
                                    }
                                )
                            }
                            
                            // 4. 如果isGetHigh=0且活跃度大于等于80，领取80活跃度礼包
                            if (activeGiftResponse.isGetHigh == 0 && currentLiveness >= 80) {
                                val getHighResult = gameApi.getActiveGiftHigh()
                                getHighResult.foldWithApiException(
                                    onSuccess = { response ->
                                        historyManager.addHistory(name, response.getDescription())
                                    },
                                    onFailure = { code, msg ->
                                        historyManager.addHistory(name, msg, code)
                                    }
                                )
                            }
                            
                            WorkResult.Success
                        },
                        onFailure = { code, msg ->
                            historyManager.addHistory(name, msg, code)
                            WorkResult.Failure(Exception(msg), msg)
                        }
                    )
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                    WorkResult.Failure(Exception(msg), msg)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            return WorkResult.Failure(e, e.message)
        }
    }
}